package com.abigdreamer.im.server.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import com.abigdreamer.im.server.log.EIMLogger;
import com.abigdreamer.im.server.module.msgProcessConfig.MessageProcessBean;
import com.abigdreamer.im.server.module.msgProcessConfig.MessageProcessConfig;
import com.abigdreamer.im.server.serverConfig.Config;
import com.abigdreamer.im.server.serverConfig.EIMChatSocketConfig;
import com.abigdreamer.im.server.serverConfig.EIMDBServerConfig;
import com.abigdreamer.im.server.serverConfig.EIMFileSocketConfig;
import com.abigdreamer.im.server.serverConfig.EIMServerConfig;
import com.abigdreamer.im.server.ui.desktop.DeskFrameConfig;
import com.abigdreamer.im.server.ui.desktop.DeskTopConfig;
import com.abigdreamer.im.server.ui.desktop.ItemConfig;
import com.thoughtworks.xstream.XStream;

public class ServerUtils {

	public static EIMServerConfig getServerConfig() {
		EIMServerConfig config=new EIMServerConfig();
		XStream xstream = getProcessServserConfigXStream();
		File file = getFile(EIMServerConfig.class.getPackage(),
		"serverConfig.xml");
		try {
			FileInputStream xml=new FileInputStream(file);
			xstream.fromXML(xml, config);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return config;
	}
    /**
     * 
     * @param fileName
     * @return
     */
	public static String getFileExtractor(String fileName){
		String[] strs=fileName.replace(".", ",").split(",");;
		return strs[strs.length-1];
	}
	
	public static boolean writeServerConfig(EIMServerConfig config){
		XStream xstream = getProcessServserConfigXStream();
		String str=xstream.toXML(config);
		File file = getFile(EIMServerConfig.class.getPackage(),
		"serverConfig.xml");
		try {
			write(file,str);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean writeProcessConfig(MessageProcessConfig config){
		XStream xstream =getProcessConfigXStream();
		String str=xstream.toXML(config);
		File file = getFile(MessageProcessConfig.class.getPackage(),"messageProcessConfig.xml");
		try {
			write(file,str);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	public static MessageProcessConfig getMessageProcessConfig(){
		MessageProcessConfig config=new MessageProcessConfig();
		XStream xstream =getProcessConfigXStream();
		File file = getFile(MessageProcessConfig.class.getPackage(),"messageProcessConfig.xml");
		try {
			FileInputStream xml=new FileInputStream(file);
			xstream.fromXML(xml, config);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return config;
	}
	
	public static boolean writeDeskTopConfig(DeskTopConfig config){
		XStream xstream =getDeskTopConfigXStream();
		String str=xstream.toXML(config);
		File file = getFile(DeskTopConfig.class.getPackage(),
		"deskTopConfig.xml");
		try {
			write(file,str);
			EIMLogger.INFO("写入了DeskTopConfig信息");
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static DeskTopConfig getDeskTopConfig(){
		DeskTopConfig config=new DeskTopConfig();
		XStream xstream =getDeskTopConfigXStream();
		Class<DeskTopConfig> s = DeskTopConfig.class;
		String filePath = s.getResource("/com/abigdreamer/im/server/ui/desktop/deskTopConfig.xml").getPath();
		System.out.println(filePath);
		File file = new File(filePath);
		
//		File file = getFile(DeskTopConfig.class.getPackage(),"deskTopConfig.xml");
		try {
			FileInputStream xml=new FileInputStream(file);
			xstream.fromXML(xml, config);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return config;
	}
	
	public static XStream getDeskTopConfigXStream(){
		XStream xstream = new XStream();
		xstream.alias("deskTop", DeskTopConfig.class);
		xstream.alias("itemConfig", ItemConfig.class);
		xstream.alias("deskFrameConfig", DeskFrameConfig.class);
		xstream.addImplicitCollection(DeskTopConfig.class, "itemList");
		xstream.useAttributeFor( DeskFrameConfig.class,"deskFrameClass");
		return xstream;
	}
	public static XStream getProcessConfigXStream(){
		XStream xstream = new XStream();
		xstream.alias("ServerProcess", MessageProcessConfig.class);
		xstream.alias("Process", MessageProcessBean.class);
		xstream.addImplicitCollection(MessageProcessConfig.class, "processBeans");
		xstream.useAttributeFor( MessageProcessBean.class,"processClass");
		return xstream;
	}
	
	public static XStream getProcessServserConfigXStream(){
		XStream xstream = new XStream();
		xstream.alias("ServerInfo", EIMServerConfig.class);
		xstream.alias("DBServer", EIMDBServerConfig.class);
		xstream.alias("chatSocketConfig", EIMChatSocketConfig.class);
		xstream.alias("fileSocketConfig", EIMFileSocketConfig.class);
		xstream.useAttributeFor(EIMChatSocketConfig.class, "name");
		xstream.useAttributeFor(EIMFileSocketConfig.class, "name");
		xstream.useAttributeFor(Config.class, "config_className");
		xstream.aliasField("socketPath", Config.class, "config_className");
		return xstream;
	}
	
	public static File getFile(Package dirPackage, String fileName) {
		String filePackage = ServerUtils.class.getResource("/").getPath() +"/" + dirPackage.getName().replace(".", "/")
				+ "/";
		File file = new File(filePackage + fileName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	public static String write(File file, String xml, String charset)
			throws IOException {
		FileOutputStream out = null;
		out = new FileOutputStream(file);
		OutputStreamWriter write = new OutputStreamWriter(out, charset);
		BufferedWriter bw = new BufferedWriter(write);
		bw.write(xml);
		bw.flush();
		bw.close();
		return xml;
	}

	public static String write(File file, String xml) throws IOException {
		write(file, xml, "UTF-8");
		return xml;
	}
	
	
	public static String fileAppend(File file, String xml, String charset)
	throws IOException {
		FileOutputStream out = null;
		out = new FileOutputStream(file,true);
		OutputStreamWriter write = new OutputStreamWriter(out, charset);
		BufferedWriter bw = new BufferedWriter(write);
		bw.append(xml);
		bw.newLine();
		bw.flush();
		bw.close();
		return xml;
	}

	public static String fileAppend(File file, String xml) throws IOException {
		    fileAppend(file, xml, "UTF-8");
			return xml;
	}
	
}
